package com.ruoyi.system.controller;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.exception.base.BaseException;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.system.domain.UserCollection;
import com.ruoyi.system.domain.vo.UserBookCollectionVo;
import com.ruoyi.system.service.IUserCollectionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 收藏Controller
 *
 * @author ruoyi
 * @date 2022-12-08
 */
@RestController
@RequestMapping("/system/collection")
public class UserCollectionController extends BaseController {
    @Autowired
    private IUserCollectionService userCollectionService;

    /**
     * 查询收藏列表
     */
    @PreAuthorize("@ss.hasPermi('system:collection:list')")
    @GetMapping("/list")
    public TableDataInfo list(UserCollection userCollection) {
        startPage();
        List<UserBookCollectionVo> list = userCollectionService.selectUserBookCollectionList(userCollection);
        return getDataTable(list);
    }

    /**
     * 导出收藏列表
     */
    @PreAuthorize("@ss.hasPermi('system:collection:export')")
    @Log(title = "导出", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, UserCollection userCollection) {
        List<UserCollection> list = userCollectionService.selectUserCollectionList(userCollection);
        ExcelUtil<UserCollection> util = new ExcelUtil<UserCollection>(UserCollection.class);
        util.exportExcel(response, list, "收藏数据");
    }

    /**
     * 获取收藏详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:collection:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(userCollectionService.selectUserCollectionById(id));
    }

    /**
     * 新增收藏
     */
    @PreAuthorize("@ss.hasPermi('system:collection:add')")
    @Log(title = "收藏", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody UserCollection userCollection) {
        userCollection.setSno(SecurityUtils.getUsername());
        // 验证是否已经收藏
        if (userCollectionService.selectUserCollectionBySnoAndBookId(userCollection.getSno(), userCollection.getBookId())) {
            throw new BaseException("已经收藏过了");
        }
        return toAjax(userCollectionService.insertUserCollection(userCollection));
    }

    /**
     * 修改收藏
     */
    @PreAuthorize("@ss.hasPermi('system:collection:edit')")
    @Log(title = "修改收藏", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody UserCollection userCollection) {
        return toAjax(userCollectionService.updateUserCollection(userCollection));
    }

    /**
     * 删除收藏
     */
    @PreAuthorize("@ss.hasPermi('system:collection:remove')")
    @Log(title = "批量删除收藏", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(userCollectionService.deleteUserCollectionByIds(ids));
    }


    /**
     * 取消收藏
     */
    @PreAuthorize("@ss.hasPermi('system:collection:remove')")
    @Log(title = "取消收藏", businessType = BusinessType.DELETE)
    @DeleteMapping("/cancel/{bookId}")
    public AjaxResult remove(@PathVariable String bookId) {
        return toAjax(userCollectionService.cancelCollect(SecurityUtils.getUsername(),bookId));
    }
}
